﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using MSWord = Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
using NPOI.XWPF.UserModel;
using System.IO;
//using EMR.BLL;
using System.Xml;



namespace EMR
{
    public partial class TemplateDesign : Form
    {

        //MSWord.Application wordApp;                   //Word应用程序变量 
        //MSWord.Document wordDoc;                  //Word文档变量

        private WinWordControl.WinWordControl objWinWordControl;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private string filNm;
        private System.Xml.XmlDocument mXmlDoc;
        private System.Xml.XmlDocument doc;


        public TemplateDesign()
        {

            InitializeComponent();

        }

        public TemplateDesign(string openxml)
        {
            InitializeComponent();
            string strFileName;
            strFileName = openxml;
            string linshi = @"../../image/临时文件.doc";
            //string linshi = "C: /Users/13128/Desktop/EMR/EMR/image/临时文件.doc";
            //MessageBox.Show(openxml);
            //If the user does not cancel, open the document.
            if (strFileName.Length != 0)
            {
                doc = new XmlDocument();
                string path = @"../../TemplateStore/" + strFileName;
                doc.Load(path);
                XmlNode node = doc.DocumentElement.SelectSingleNode("me");
                XmlElement ele = (XmlElement)node;//获取一个xml元素
                string pic = ele.GetAttribute("aa");//获取ele元素的aa属性并报讯在一个临时字符串变量pic
                byte[] bytes = Convert.FromBase64String(pic);//声明一个byte[]用来存放Base64解码转换过来的数据流
                FileStream outfile = new System.IO.FileStream(linshi, System.IO.FileMode.CreateNew);
                outfile.Write(bytes, 0, (int)bytes.Length);
              //  MessageBox.Show(linshi);
                outfile.Close();
                outfile.Dispose();
              
            }
            // InitializeComponent();
            try
            {
                objWinWordControl.CloseControl();

            }
            catch { }
            finally
            {
                objWinWordControl.document = null;
                WinWordControl.WinWordControl.wd = null;
                WinWordControl.WinWordControl.wordWnd = 0;
            }
            try
            {

                //Load the template used for testing.
                objWinWordControl.LoadDocument(linshi);
                //  System.Diagnostics.Process.Start(linshi);
                // this.Cursor = System.Windows.Forms.Cursors.Arrow;//设置鼠标为正常状态
            }
            catch (Exception ex) { var err = ex.Message; }
            try
            {
                System.Diagnostics.Process.Start(@"C:\Users\13128\Desktop\EMR\EMR\image\临时文件.doc");
            }
            catch { }
          
        }

        /// <summary>
        /// 打开
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Open_Click(object sender, EventArgs e)
        {

           // string filNm;
            openFileDialog1.Multiselect = false;
            openFileDialog1.Filter = "MS-Word Files (*.doc,*.dot,*docx) | *.doc;*.dot;*docx";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                filNm = openFileDialog1.FileName;
            }
            else return;
            this.Cursor = System.Windows.Forms.Cursors.WaitCursor;//鼠标为忙碌状态

            try
            {
                objWinWordControl.CloseControl();
                

            }
            catch { }
            finally
            {
                objWinWordControl.document = null;
                WinWordControl.WinWordControl.wd = null;
                WinWordControl.WinWordControl.wordWnd = 0;
            }
            try
            {

                //Load the template used for testing.
                objWinWordControl.LoadDocument(filNm);
                // MessageBox.Show(filNm);
                this.Cursor = System.Windows.Forms.Cursors.Arrow;//设置鼠标为正常状态
            }
            catch (Exception ex) { var err = ex.Message; }
        }

        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TemplateDesign_FormClosed(object sender, FormClosedEventArgs e)
        {
            objWinWordControl.RestoreCommandBars();
            objWinWordControl.CloseControl();
        }

        //private void New_Build()
        //{
        //    XWPFDocument n_Docx = new XWPFDocument();

        // }

            /// <summary>
            /// 测试
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void btn_AddList_Click(object sender, EventArgs e)
        {

            MessageBox.Show("能用");
        }
        /// <summary>
        /// 关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Close_Click(object sender, EventArgs e)
        {
            objWinWordControl.RestoreCommandBars();
            objWinWordControl.CloseControl();
        }



        private void btn_Save_Click(object sender, EventArgs e)
        {
            string strFileName;
            //openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt";
            //openFileDialog1.FilterIndex = 1;
            //openFileDialog1.FileName = "";
            //openFileDialog1.ShowDialog();
            strFileName = openFileDialog1.FileName;
            //objWinWordControl.RestoreCommandBars();
            //objWinWordControl.CloseControl();  //关闭
            if (strFileName.Length != 0)
            {
                System.IO.FileStream inFile = new FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                byte[] binaryData = new byte[inFile.Length];
                inFile.Read(binaryData, 0, (int)inFile.Length);
                string mStr = Convert.ToBase64String(binaryData);
                string hh = mStr;
                mXmlDoc = new System.Xml.XmlDocument();
                mStr = string.Format("<wawa><me aa=\"{0}\"/></wawa>", mStr); //wawa可改 aa要和后面一起改
                mXmlDoc.LoadXml(mStr);
                // MessageBox.Show(strFileName); 
                mXmlDoc.Save($"../../TemplateStore/标准模板/3.xml"); //保存
                MessageBox.Show("保存成功");


            }
            //saveFileDialog1.Filter = "xml 文件|*.xml";//设置打开对话框的文件过滤条件
            //saveFileDialog1.Title = "保存成 xml 文件";//设置打开对话框的标题
            //saveFileDialog1.FileName = "";
            //saveFileDialog1.ShowDialog();//打开对话框
            //if (saveFileDialog1.FileName != "")//检测用户是否输入了保存文件名
            //{
            //    mXmlDoc.Save(saveFileDialog1.FileName);//用私有对象mXmlDoc保存文件,mXmlDoc在前面声明过
            //    
            //}
           
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {

           // System.Diagnostics.Process.Start(@"C:\Users\13128\Desktop\EMR\EMR\image\临时文件.doc"); 
        }
    }
}
